package cn.com.ebidding.controller;

import cn.com.ebidding.business.annotation.BussinessLog;
import cn.com.ebidding.business.entity.Group;
import cn.com.ebidding.business.entity.UserGroup;
import cn.com.ebidding.business.enums.ResponseStatus;
import cn.com.ebidding.business.enums.UserStatusEnum;
import cn.com.ebidding.business.service.SysGroupService;
import cn.com.ebidding.business.service.SysUserGroupService;
import cn.com.ebidding.business.vo.GroupConditionVO;
import cn.com.ebidding.business.vo.UserGroupConditionVO;
import cn.com.ebidding.framework.object.PageResult;
import cn.com.ebidding.framework.object.ResponseVO;
import cn.com.ebidding.util.ResultUtil;
import cn.com.ebidding.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.Session;
import java.util.Date;

/**
 * 用户管理
 *
 * @author 扫地僧 (saods@ebidding.com.cn)
 * @version 1.0
 * @website https://ksp.ebidding.com.cn
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@RestController
@RequestMapping("/user_group")
public class RestUserGroupController {
    @Autowired
    private SysUserGroupService groupService;

    @RequiresPermissions("usergroups")
    @PostMapping("/list/{groupId}")
    public PageResult list(UserGroupConditionVO vo,@PathVariable("groupId") Long groupId) {
        if (vo.getUserGroup() == null) vo.setUserGroup(new UserGroup());
        vo.getUserGroup().setGroupId(groupId);
        PageInfo<UserGroup> pageInfo = groupService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }


    @RequiresPermissions("usergroup:add")
    @PostMapping(value = "/add")
    @BussinessLog("添加用户")
    public ResponseVO add(UserGroup userGroup) {
        UserGroup u = groupService.findUser(userGroup);
        if(u.getUserGroup() != null){
            if(u.getUserGroup().getStatus() == 0){
                u.getUserGroup().setStatus(1);
                u.getUserGroup().setUpdateUser(SessionUtil.getUser().getId());
                u.getUserGroup().setUpdateTime(new Date());
                groupService.updateSelective(u);
                return ResultUtil.success("操作成功！");
            }else {
                return ResultUtil.error("当前用户已存在！");
            }

        }
        try {
            groupService.add(userGroup);
            return ResultUtil.success("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("error");
        }
    }



    @RequiresPermissions("usergroup:get")
    @PostMapping("/get/{id}")
    @BussinessLog("获取分组详情")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.groupService.getByPrimaryKey(id));
    }



    @RequiresPermissions("usergroup:delete")
    @PostMapping(value = "/remove")
    @BussinessLog("删除用户")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        UserGroup group = new UserGroup();
        group.setStatus(UserStatusEnum.DISABLE.getCode());
        for (Long id : ids) {
            group.setId(id);
            groupService.updateSelective(group);
            //todo  删除用户与组的关联
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个用户！");
    }


}
